import os
import sys
import random
import time
import Mk_soldier
import skills
import skills_fun

def enable_skills(magic,owner_skills):
    en_skills = []
    if magic >= 5 and magic <=10:
        for i in range(0, len(owner_skills)):
            if owner_skills[i] in skills.skill_list(magic):
                en_skills.append(owner_skills[i])
        return en_skills
    elif magic < 5 and magic > 0:
        return en_skills

def sub_libreate_skills(magic, aggressor_blood, sufferer_blood,
               aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu,
               sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,
               aggressor, sufferer, aggressor_skills,check_skill_num):
    aggressor_en_skills = enable_skills(magic, aggressor_skills)
    while True:
        if check_skill_num < len(aggressor_en_skills):
            if aggressor_en_skills[check_skill_num] == 'double_hit':
                sufferer_blood, magic = skills.double_hit(sufferer_blood, magic, aggressor,
                                                          sufferer)
                return magic, aggressor_blood, sufferer_blood, \
                       aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
                       sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'double_hit'
            elif aggressor_en_skills[check_skill_num] == 'get_dizziness':
                sufferer_blood, sufferer_dizziness_stu, magic = skills.get_dizziness(sufferer_blood, sufferer_dizziness_stu, magic,
                                                                     aggressor, sufferer)
                return magic, aggressor_blood, sufferer_blood, \
                       aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
                       sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'
            elif aggressor_en_skills[check_skill_num] == 'get_silence':
                sufferer_silence_stu, magic = skills.get_silence(sufferer_silence_stu, magic,
                                                                 aggressor, sufferer)
                return magic, aggressor_blood, sufferer_blood, \
                       aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
                       sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'
            elif aggressor_en_skills[check_skill_num] == 'get_poison':
                sufferer_blood, sufferer_poison_stu, magic = skills.get_poison(sufferer_blood,
                                                                               sufferer_poison_stu,
                                                                               magic,
                                                                               aggressor,
                                                                               sufferer)
                return magic, aggressor_blood, sufferer_blood, \
                       aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
                       sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'
            elif aggressor_en_skills[check_skill_num] == 'get_health':
                aggressor_blood, magic = skills.get_health(aggressor_blood, magic, aggressor)
                return magic, aggressor_blood, sufferer_blood, \
                       aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
                       sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'get_health'
            else:
                return magic, aggressor_blood, sufferer_blood, \
                       aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
                       sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'

        elif check_skill_num == len(aggressor_en_skills):
            return magic, aggressor_blood, sufferer_blood, \
                   aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
                   sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'

def user_liberate_skills(magic, aggressor_blood, sufferer_blood,
               aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu,
               sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,
               aggressor, sufferer, aggressor_skills):
    if magic >= 5 and aggressor_dizziness_stu == 0 and aggressor_silence_stu == 0:
        while True:
            print("are you use skill?")
            aggressor_en_skills = enable_skills(magic, aggressor_skills)
            for i in range(0, len(aggressor_en_skills)):
                print('\t%s.\033[34m%s\033[0m' % (i, aggressor_en_skills[i].title()))
            print("\t%s.\033[34ma hit\033[0m" % len(aggressor_en_skills))
            try:
                check_skill_num = int(input("you choose "))
                if check_skill_num > len(aggressor_en_skills) or check_skill_num < 0:
                    print("\nPlease input number 0~%s" % len(aggressor_en_skills))
                    continue
            except ValueError:
                print("\nPlease input number 0~%s" % len(aggressor_en_skills))
                continue
            magic, aggressor_blood, sufferer_blood, \
            aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
            sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,skill_name = \
                sub_libreate_skills(magic, aggressor_blood, sufferer_blood,
                                    aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu,
                                    sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,
                                    aggressor, sufferer, aggressor_skills, check_skill_num)
            return magic, aggressor_blood, sufferer_blood, \
            aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
            sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,skill_name
    else:
        return magic, aggressor_blood, sufferer_blood, \
               aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
               sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'

def during_poison_fun(blood, poison_stu, aggressor):
    if poison_stu > 0:
        print(' ')
        blood, poison_stu = skills_fun.fun_poison(blood, poison_stu, aggressor)
        return blood,poison_stu
    else:
        return blood,poison_stu

def during_dizziness_fun(dizziness_stu, aggressor):
    if dizziness_stu > 0:
        print(' ')
        dizziness_stu = skills_fun.fun_dizziness(dizziness_stu, aggressor)
        return dizziness_stu
    else:
        return dizziness_stu

def during_silence_fun(silence_stu, aggressor):
    if silence_stu > 0:
        print(' ')
        silence_stu = skills_fun.fun_silence(silence_stu, aggressor)
        return silence_stu
    else:
        return silence_stu

def com_libr_skill(magic, aggressor_blood, sufferer_blood,
               aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu,
               sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,
               aggressor, sufferer, aggressor_skills):
    if magic >= 5 and aggressor_dizziness_stu == 0 and aggressor_silence_stu == 0:
        while True:
            aggressor_en_skills = enable_skills(magic, aggressor_skills)
#            if len(aggressor_en_skills) == 0:
#                print'not enough magic!'
#                return magic, aggressor_blood, sufferer_blood, \
#                       aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
#                       sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'
#                break
            check_skill_b = int(100 * random.random()) % (len(aggressor_en_skills) + 1)

            magic, aggressor_blood, sufferer_blood, \
            aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
            sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,skill_name = \
                sub_libreate_skills(magic, aggressor_blood, sufferer_blood,
                                    aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu,
                                    sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,
                                    aggressor, sufferer, aggressor_skills, check_skill_b)
#            print('check_skill_b : %s\nskill_name : %s' % (check_skill_b,skill_name))
            return magic, aggressor_blood, sufferer_blood, \
            aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
            sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu,skill_name
    else:
        return magic, aggressor_blood, sufferer_blood, \
               aggressor_dizziness_stu, aggressor_silence_stu, aggressor_poison_stu, \
               sufferer_dizziness_stu, sufferer_silence_stu, sufferer_poison_stu, 'pk_info'


